package com.db.compare.controller;

import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseController;
import com.db.compare.core.BaseResponse;
import com.db.compare.mapper.RbacMenuButtonMapper;
import com.db.compare.model.RbacMenuButton;
import com.db.compare.service.RbacMenuButtonService;
import com.db.compare.vo.RbacMenuButtonVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
* 菜单按钮管理
* */
@Slf4j
@Controller
@RequestMapping("/rbac/menu/button")
public class RbacMenuButtonController extends BaseController{

    @Autowired
    private RbacMenuButtonMapper rbacmenubuttonMapper;
    @Autowired
    private RbacMenuButtonService rbacmenubuttonService;

    @RequestMapping(value={"/list",""})
    public String rbacmenubuttonList(){
        return "/html/RbacMenuButton";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<RbacMenuButton> pages(Model model,@RequestParam Map rbacmenubuttonMap){
        RbacMenuButtonVo rbacmenubuttonVo = JSON.parseObject(JSON.toJSONString(rbacmenubuttonMap), RbacMenuButtonVo.class);
        return rbacmenubuttonService.pageInfo(rbacmenubuttonVo);
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        rbacmenubuttonMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody RbacMenuButton rbacmenubutton){
        if(rbacmenubutton.getId()==null) {
            rbacmenubuttonMapper.insert(rbacmenubutton);
        }else{
            rbacmenubuttonMapper.updateByPrimaryKey(rbacmenubutton);
        }
        return new BaseResponse(200,rbacmenubutton);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        RbacMenuButton rbacmenubutton = rbacmenubuttonMapper.selectByPrimaryKey(pk);
        return new BaseResponse(200,rbacmenubutton);
    }
    //下载模板
    @RequestMapping("/download")
    public void testRespExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String ids=request.getParameter("ids");
        rbacmenubuttonService.download(ids,response);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public BaseResponse upload(@RequestParam("file") MultipartFile file){
        if(!rbacmenubuttonService.upload(file)){
            return buildFailResponse();
        }

        return buildSuccessResponse();
    }
}